package com.wondersgroup.core.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wondersgroup.core.frame.TraceException;
import com.wondersgroup.core.mapper.RegionMapper;
import com.wondersgroup.core.model.Region;
import com.wondersgroup.core.util.ErrorMessageConstant;

@Service
public class RegionService {

	private final Logger logger = LoggerFactory.getLogger(RegionService.class);

	@Autowired
	private RegionMapper regionMapper;

	public List<Region> selectByParentId(int parentId) {
		return regionMapper.selectByParentId(parentId);
	}

	public List<Region> selectByAdminLevel(int adminLevel) {
		// TODO Auto-generated method stub
		return regionMapper.selectByAdminLevel(adminLevel);
	}

	public List<Region> selectByCode(String code) {
		// TODO Auto-generated method stub
		return regionMapper.selectByCode(code);
	}

	@Transactional(readOnly = false)
	public Region getRegionById(Integer id) throws TraceException {
		if (id == null) {
			throw TraceException
					.returnException(ErrorMessageConstant.CODE_PARAM_ERROR);
		}
		return regionMapper.selectByPrimaryKey(id);
	}
	public List<Region> queryRegionListByCondition(Integer parentId, Integer adminLevel){
    	Region region = new Region();
    	if(parentId != null){
    		region.setParentId(parentId);
    	}
    	if(adminLevel != null && adminLevel > 0){
    		region.setAdminLevel(adminLevel);
    	}
    	
        return regionMapper.select(region);
    }
    
}
